﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace ReportingManager
{
    public partial class frmRaporFiltre : DevExpress.XtraEditors.XtraForm
    {
        private string _rapor_sql;
        private string _orjinal_sql;

        public frmRaporFiltre()
        {
            InitializeComponent();
        }

        private void C1FlexGrid1_BeforeEdit(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
        {
            if (C1FlexGrid1[e.Row, 2] != null)
            {
                if (C1FlexGrid1[e.Row, 2].ToString() == "D")
                {
                    C1FlexGrid1.Cols[1].DataType = typeof(DateTime);
                }
                else
                {
                    C1FlexGrid1.Cols[1].DataType = typeof(string);
                }
            }
        }


        #region Func
        private void Satirekle(string alanadi, string tip, bool rowvisible)
        {
            int rw = C1FlexGrid1.Rows.Count - 1;
            C1FlexGrid1[rw, 0] = alanadi;
            C1FlexGrid1[rw, 2] = tip;
            //C1FlexGrid1.Rows[rw].Visible = rowvisible;
            C1FlexGrid1.Rows.Add();
        }
        private void Satirekle(string alanadi, string tip, string fonksiyon, bool rowvisible)
        {
            int rw = C1FlexGrid1.Rows.Count - 1;
            C1FlexGrid1[rw, 0] = alanadi;
            C1FlexGrid1[rw, 2] = tip;
            C1FlexGrid1[rw, 3] = fonksiyon;
            //C1FlexGrid1.Rows[rw].Visible = rowvisible;
            C1FlexGrid1.Rows.Add();
        }
        private void RaporSql()
        {
            string reptext = "";
            string yenideger = "";
            _rapor_sql = _orjinal_sql;
            System.Collections.Generic.Dictionary<string, string> dic = new System.Collections.Generic.Dictionary<string, string>();
            for (int i = 0; i <= C1FlexGrid1.Rows.Count - 1; i++)
            {
                if (C1FlexGrid1[i, 0] != null)
                {
                    if (C1FlexGrid1[i, 3] == null)
                    {
                        reptext = "@" + C1FlexGrid1[i, 2] + "@" + C1FlexGrid1[i, 0] + "@";
                    }
                    else
                    {
                        reptext = "@" + C1FlexGrid1[i, 3] + "@" + C1FlexGrid1[i, 2] + "@" + C1FlexGrid1[i, 0] + "@";
                    }

                    if (C1FlexGrid1[i, 3] == null)
                    {
                        if (C1FlexGrid1[i, 2].ToString() == "D")
                        {
                            DateTime tarih = (DateTime)C1FlexGrid1[i, 1];
                            yenideger = tarih.Year + "-" + tarih.Month + "-" + tarih.Day;
                        }
                        else
                        {
                            yenideger = C1FlexGrid1[i, 1].ToString();
                        }
                        _rapor_sql = _rapor_sql.Replace(reptext, yenideger);
                    }
                    else
                    {
                        if (C1FlexGrid1.Rows[i].Visible == false)
                        {
                            if (C1FlexGrid1[i, 2].ToString() == "T")
                            {
                                yenideger = Fonksiyon(C1FlexGrid1[i, 3].ToString(), dic[C1FlexGrid1[i, 0].ToString()]);
                                _rapor_sql = _rapor_sql.Replace(reptext, yenideger);
                            }
                        }
                        else
                        {
                            if (C1FlexGrid1[i, 2].ToString() == "T")
                            {
                                if (C1FlexGrid1[i, 3] != null)
                                    yenideger = Fonksiyon(C1FlexGrid1[i, 3].ToString(), C1FlexGrid1[i, 1].ToString());
                                else
                                    yenideger = C1FlexGrid1[i, 1].ToString();

                                _rapor_sql = _rapor_sql.Replace(reptext, yenideger);
                                dic.Add(C1FlexGrid1[i, 0].ToString(), C1FlexGrid1[i, 1].ToString());
                            }
                        }

                    }
                }
            }
        }
        private string Fonksiyon(string fnkAdi, string deger)
        {
            string sonuc = "";
            switch (fnkAdi)
            {
                case "fn_ilkgun":
                    sonuc = DateTime.Now.Year + "-" + Int32.Parse(deger).ToString("00") + "-01 00:00:00";
                    break;
                case "fn_songun":
                    DateTime tarih = default(DateTime);
                    if (Int32.Parse(deger) < 12)
                    {
                        tarih = new DateTime(DateTime.Now.Year, Int32.Parse(deger) + 1, 1);
                    }
                    else
                    {
                        tarih = new DateTime(DateTime.Now.Year + 1, 1, 1);
                    }
                    tarih = tarih.AddDays(-1);
                    sonuc = tarih.Year + "-" + tarih.Month + "-" + tarih.Day + " 23:59:59";
                    break;
            }
            return sonuc;
        }
        private void RaporOku()
        {
            if (File.Exists(Config.Get.CurrentReportFile) == true)
            {
                StreamReader sr = new StreamReader(Config.Get.CurrentReportFile, System.Text.Encoding.Default);
                string line = "";
                string[] ozellik = null;
                string tip = null;
                string alanad = null;
                string fonksiyon = null;
                System.Collections.Generic.List<string> lst = new System.Collections.Generic.List<string>();

                while (sr.Peek() > -1)
                {
                    line = sr.ReadLine();
                    _orjinal_sql += (line + " \r\n");
                    if (line.IndexOf('@') > -1)
                    {
                        ozellik = line.Split('@');
                        if (ozellik.Length > 2 & ozellik.Length < 5)
                        {
                            tip = ozellik[1];
                            alanad = ozellik[2];
                            if (lst.Contains(alanad) == false)
                            {
                                Satirekle(alanad, tip, true);
                                lst.Add(alanad);
                            }
                            //else
                            //{
                            //    Satirekle(alanad, tip, false);
                            //}
                        }
                        else if (ozellik.Length >= 5)
                        {
                            tip = ozellik[2];
                            alanad = ozellik[3];
                            fonksiyon = ozellik[1];
                            if (lst.Contains(alanad) == false)
                            {
                                Satirekle(alanad, tip, fonksiyon, true);
                                lst.Add(alanad);
                            }
                            //else
                            //{
                            //    Satirekle(alanad, tip, fonksiyon, false);
                            //}
                        }
                    }
                }
                sr.Close();
            }
        }
        #endregion

        private void frmRaporFiltre_Load(object sender, EventArgs e)
        {
            RaporOku();
            C1FlexGrid1.Cols[2].Visible = false;
            C1FlexGrid1.Cols[3].Visible = false;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (File.Exists(Config.Get.Path + "\\" + Config.Get.RaporAdi + ".xml"))
            {
                RaporSql();
                frmRaporGoruntule frm = new frmRaporGoruntule();
                frm.DataSource = Database.DataSender(_rapor_sql);
                frm.Show();
                this.Close();
            }
            else
                simpleButton2_Click(simpleButton2, null);
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            RaporSql();
            frmKolonAyar ilk = new frmKolonAyar();
            ilk.SqlQuery = _rapor_sql;
            ilk.Show();
            this.Close();
        }

    }
}